﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace MoreThread
{
    class Program
    {

        #region ThreadCosts
        //static void Main()
        //{
        //    Console.WriteLine("Hello Thread World!");
        //    var thds = new List<System.Threading.Thread>();
        //    var thdnum = 10000;  // 线程数量
        //    for (int i = 0; i < thdnum; i++)
        //    {
        //        var t = new System.Threading.Thread(TestThread);
        //        t.Start();
        //        thds.Add(t);
        //    }

        //    Console.WriteLine("Thread Finished!");
        //    Console.ReadKey();
        //}

        //static void TestThread()
        //{
        //    Console.ReadKey();
        //}

        //static void Main(string[] args)
        //{
        //    Console.WriteLine("Hello Thread World!");

        //    var dts = MakeData();
        //    var sw = new Stopwatch();
        //    sw.Start();
        //    SingleThread(dts);
        //    sw.Stop();
        //    Console.WriteLine($"Single Thread Time Costs: {sw.ElapsedTicks}");

        //    sw.Restart();
        //    MultiThread(dts);
        //    sw.Stop();
        //    Console.WriteLine($"Multi Thread Time Costs: {sw.ElapsedTicks}");

        //    Console.ReadKey();
        //}

        static void SingleThread(IList<Data> datas)
        {
            foreach (var item in datas)
            {
                item.DoSomeThing();
            }
        }

        static void MultiThread(IList<Data> datas)
        {
            foreach (var item in datas)
            {
                var thd = new System.Threading.Thread(
                    () => item.DoSomeThing());
                thd.Start();

                // 这里先不管 thd.Join();
                // 也不要求 DoSomeThing 始终执行
            }
        }
        #endregion

        #region ThreadPool
        public static void Main()
        {
            Console.WriteLine("Hello Thread World!");

            var dts = MakeData();

            MultiThreadPool(dts);

            Console.ReadKey();
        }

        static void MultiThreadPool(IList<Data> datas)
        {
            foreach (var item in datas)
            {
                ThreadPool.QueueUserWorkItem(
                    obj => item.DoSomeThing());
                // 我们无法控制 ThreadPool 中的线程的 开始、挂起、和中止等
                // 这个需要注意的地方
            }
        }

        #endregion


        static IList<Data> MakeData()
        {
            var dataNum = 20; // 任务数量
            var datas = new List<Data>(dataNum);
            for (int i = 0; i < dataNum; i++)
            {
                datas.Add(new Data(i));
            }

            return datas;
        }


    }

    class Data
    {
        public Data(int id)
        {
            ID = id;
        }
        public int ID { get; set; }

        public void DoSomeThing()
        {
            ID += 1;

            Console.WriteLine(
                $"Thread ID:{Thread.CurrentThread.ManagedThreadId}" +
                $"\t Data ID:{ID}");
        }
    }
}
